<?php
/*
 * @分销商城
 * @功能说明：分类主体模块
 * @更新说明：暂无更新
 * @文件名 CateController.class.php
 * @编码 UTF-8
 * @创建时间 2015-12-25 上午11:18:01
 * @创建人 XING
 */
namespace dg\Controller;
use Think\Controller;
class CateController extends ApibaseController {
	/*
	 * 分销商城_层级管理
	 * @date: 2015-12-25 上午11:18:25
	 * @editor: XING
	 */
    public function index(){
    	if(I('get.ajaxFrom')){
    		$cates = M('CateEntity')->where(array(
    					'parent_id' => I('get.cateid')
    				))->order(array('sort'=>'desc'))->select();
    		if($cates){
    			foreach($cates AS &$val){
    				$val['add_at'] = date('Y-m-d');
    				$val['iSon'] = M('CateEntity')->where(array(
    						'parent_id' => $val['cate_id']
    					))->count();
    				$val['logo_url'] = transUrl($val['logo_url']) ?: '/statics/images/nopic.gif';
    				$val['levels'] = $val['levels'] ?: '-';
    			}
    		}
    		jsonRespons($cates ?: 0);
    	}
    	$cateModel = M('CateEntity')->where(array('parent_id'=>'0'));
    	if(I('get.catename'))
    		$cateModel = $cateModel->where('cate_name LIKE "%'.I('get.catename').'%"');
    	$cateList = $cateModel->order(array('sort'=>'desc','add_at'=>'desc'))->select();
    	$this->assign('cateList',$cateList);
    	$this->display('cate/index');
    }
    
	/*
	 * 分销商城_层级管理添加
	 * @date: 2015-12-25 上午11:18:35
	 * @editor: XING
	 */
    public function addcate(){
    	/*分类级别自动判断*/
    	$getCateId = I('get.cateid') ?: time();
    	$getClevel = function($getCateId){
    		for($sid=0;$sid<10;$sid++){
    			$getCateId = M('CateEntity')->getFieldByCateId($getCateId,'parent_id');
    			if(!$getCateId){
    				return $sid;
    				break;
    			}
    		}
    	};
    	$sid = $getClevel($getCateId);
    	$ssid = $sid ?: 2;
    	if(I('post.dosubmit')){
    		$data = array(
    				'cate_name' => I('post.catename'),
    				'parent_id' => I('post.cateid'),
    				'update_at' => time(),
    				'sort' => I('post.sort'),
    				'logo_url' => trim(I('post.logo_url')),
    				'spread_pic' => trim(I('post.spread_pic')),
    				'spread_name' => trim(I('post.spread_name')),
    				'spread_status' => trim(I('post.spread_status')),
    			);
    		if(I('post.cate_id')){
    			$getOldLevel = M('CateEntity')->getFieldByCateId(I('post.cate_id'),'parent_id');
    			 $affect = M('CateEntity')->where(array(
    			 				'cate_id' => I('post.cate_id')
    			 		   ))->setField($data);
    			 if($affect){
    			 	if(I('post.cateid')){
    			 	M('CateEntity')->where(array(
		    			 		'cate_id' => I('post.cateid')
		    			 	))->setInc('levels',1);
    			 	}
    			 	if($getOldLevel){
    			 		M('CateEntity')->where(array(
	    			 			'cate_id' => $getOldLevel
	    			 		))->setDec('levels',1);
    			 	}	
    			 	putlog('层级分类编辑 ID`'.I('post.cate_id').'`','IndexController','addcate');
    			 	$this->showtips('分类编辑成功');
    			 }else{
    			 	$this->showtips('操作失败');
    			 }
    		}
    		
			$data['add_at'] = time();
			$getAddId = M('CateEntity')->add($data);
			if($getAddId){
				if(I('post.cateid')){
					M('CateEntity')->where(array(
    			 				'cate_id' => I('post.cateid')
    			 		   ))->setInc('levels',1);
				}
				putlog('层级分类添加 `'.I('post.catename').'`','IndexController','addcate');
				$this->showtips('分类添加成功');
			}else{
				$this->showtips('操作失败');
			}
    	}elseif(I('get.cateid')){
    		$cateRow = M('CateEntity')->where(array(
    				'cate_id'=>I('get.cateid')
    			))->find();
    		
    		$this->assign('cateRow',$cateRow);
    		$this->assign('cateEntity',getCates($ssid));
    		$this->display('cate/addcate');
    	}
    	
    	$this->assign('cateEntity',getCates($ssid));
    	$this->display('cate/addcate');
    }
    
	/*
	 * 分销商城_分类层级删除
	 * @date: 2015-12-25 上午11:18:51
	 * @editor: XING
	 */
    public function deletecate(){
    	if(I('get.cateid')){
    		M('CateEntity')->where(array('parent_id'=>I('get.cateid')))->delete();
    		M('CateEntity')->where(array('cate_id'=>I('get.cateid')))->delete();
    		M('GoodsCate')->where(array('cate_id'=>I('get.cateid')))->delete();
    		putlog('层级分类删除 ID`'.I('get.cateid').'`','IndexController','deletecate');
    		$this->showtips('分类删除成功');
    	}
    }
}